package com.example.compose

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.tooling.preview.Preview

@Preview
@Composable
fun MultiTouchCanvas1() {
    var touchPoints by remember { mutableStateOf(emptyList<Offset>()) }

    Canvas(
        modifier = Modifier
            .fillMaxSize()
            .pointerInput(Unit) {
                awaitEachGesture {
                    while (true) {
                        val event = awaitPointerEvent()
                        // 获取所有触控点的坐标
                        val points = event.changes.map { it.position }
                        println(points)
                        println(points.size)
                        // 更新触控点的列表
                        touchPoints = points
                    }
                }
            }
    ) {
        drawIntoCanvas { canvas ->
            drawTouchPoints(canvas, touchPoints)
        }
    }
}

private fun drawTouchPoints(canvas: Canvas, points: List<Offset>) {
    canvas.apply {
        for (point in points) {
            drawCircle(paint = Paint(), radius = 30f, center = point)
        }
    }
}

